package com.chuanjiabao.wisdom.ui.fragment;

import android.annotation.SuppressLint;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSSClient;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyTextCallbackBean;
import com.chuanjiabao.wisdom.databinding.FragmentFindToSeeBinding;
import com.chuanjiabao.wisdom.util.GlideUtil;
import com.chuanjiabao.wisdom.util.LogUtil;

@SuppressLint("ValidFragment")
public class FindToSeeFragment extends BaseLazyFragment {

    FragmentFindToSeeBinding binding;
    FamilyTextCallbackBean.DataBeanX.DataBean bean;
    OSSClient oss;

    public FindToSeeFragment(OSSClient oss, FamilyTextCallbackBean.DataBeanX.DataBean bean) {
        this.bean = bean;
        this.oss = oss;
    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_find_to_see;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_find_to_see, container, false);
        binding.setFragment(this);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        binding.jvModule.getBackButton()
                .setVisibility(View.GONE);
        binding.jvModule.getFullscreenButton()
                .setOnClickListener(
                        v -> binding.jvModule.startWindowFullscreen(getActivity(), true, true)
                );

        if (bean != null) {
            switch (bean.getFile_type()) {
                case 1:
                    binding.ivModuleImg.setVisibility(View.VISIBLE);
                    binding.jvModule.setVisibility(View.GONE);
                    binding.tvModuleText.setVisibility(View.GONE);

                    try {
                        String url = oss.presignConstrainedObjectURL(
                                bean.getFile_bucket(),
                                bean.getFile(),
                                30 * 60
                        );

                        GlideUtil.setUrlImg(
                                getActivity(),
                                url,
                                bean.getFile() + "/original",
                                binding.ivModuleImg
                        );
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    binding.ivModuleImg.setVisibility(View.GONE);
                    binding.jvModule.setVisibility(View.VISIBLE);
                    binding.tvModuleText.setVisibility(View.GONE);

                    try {
                        String url = oss.presignConstrainedObjectURL(
                                bean.getFile_bucket(),
                                bean.getFile(),
                                30 * 60
                        );

                        binding.jvModule.setUp(url, true, "");
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }
                    break;
                case 4:
                    LogUtil.e("文本");
                    binding.ivModuleImg.setVisibility(View.GONE);
                    binding.jvModule.setVisibility(View.GONE);
                    binding.tvModuleText.setVisibility(View.VISIBLE);
                    binding.tvModuleText.setText(bean.getContent());
                    break;
            }
        }
    }

    @Override
    protected void onVisible() {


    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    public void onResume() {
        super.onResume();
        binding.jvModule.onVideoResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        binding.jvModule.onVideoPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding.jvModule.release();
    }
}
